<?php
$header_msg = 'My Page';
require_once ('db.php');
include_once('header.inc');
$sql = "SELECT `username` FROM `users` WHERE
			`id`='".$_SESSION['USER_ID']."'";
$authstate = $eebsdb->query($sql) or die('could not find user in DB');
$author = $authstate->fetch();

$sql = "SELECT `email` FROM `users` WHERE
			`id`='".$_SESSION['USER_ID']."'";
$authstate = $eebsdb->query($sql) or die('could not find user in DB');
$authemail = $authstate->fetch();

// Brukes for nytt image
 if ((!empty($_POST['img_url']))) {
	$stimg = $eebsdb->prepare("UPDATE `users` 
							SET imgurl='".$_POST['img_url']."'
							WHERE id='".$_SESSION['USER_ID']."'");
	$stimg->execute() or die("database img insert query not successfull");
 }

$sql = "SELECT `imgurl` FROM `users` WHERE
			`id`='".$_SESSION['USER_ID']."'";
$authstate = $eebsdb->query($sql) or die('could not find user in DB');
$authimg = $authstate->fetch();

if (!empty($_SESSION['USER_ID'])) {

 if (($authimg['imgurl'] === '0') || (empty($authimg['imgurl']))) {
	?><IMG src="http://www.minecraftforum.net/uploads/d424a80f76e16bf552a09fae02fee808_335090.jpg"><?php
}else{
	?><IMG src="<?php echo $authimg['imgurl'] ?>"><?php
}
?>
	<form method="post" action="mypage.php">
		<label for="img_url">New Image Url: </label>
			<input type="url" name="img_url" title="Urlen for shoo"><br/>
		<input type="submit" value="Upload new img"/>
	</form>
<?php
	echo 	"<P>
				User: " . $author['username'] . "<br><br>
				User e-mail: " . $authemail['email'] .
			"</P>";			
	$blogdbstatement = $eebsdb->query("SELECT * FROM `blogentries`
		ORDER BY -`created_date`")
		or die("<P>DB query for blogentries unsuccessfull. usually this is because
		the blog-post you are trying to read does not exist, or there are no
		blog-posts posted yet.</P>");
//		echo "<P>ladida before for loop</P>";
	echo "My blogs: ";
	for ($blgvar = 0; $blgvar < $blogdbstatement->rowCount(); ++$blgvar) {
		$currpost = $blogdbstatement->fetch();
//		print_r($currpost);
		$sql = "SELECT `username` FROM `users` WHERE
			`id`='".$currpost['user_id']."'";
		$authstate = $eebsdb->query($sql) or die('could not find user in DB');
		$postauthor = $authstate->fetch();
		if ($postauthor['username'] === $author['username']) {
		?>
		<H3><A HREF="readblog.php?blognumber=<?PHP echo $currpost['id']?>">
			<?PHP echo $currpost['title']; ?></A></H3>
		<?PHP
		}
	} // for '$blgvar' end
}else {
}
?>
